//
//  BaseQWViewController.m
//  WJCommon-master
//
//  Created by Yunhai.Wu on 16/1/15.
//  Copyright © 2016年 WJ. All rights reserved.
//

#import "BaseQWViewController.h"

@interface BaseQWViewController ()<UIGestureRecognizerDelegate>

@end

@implementation BaseQWViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self qw_refreshUIElement];
}

-(void)setQwNavTitle:(NSString *)qwNavTitle {
    if (_qwNavTitle == qwNavTitle) {
        return;
    }
    _qwNavTitle = [qwNavTitle copy];
    [_qwNavigationItem setTitle:_qwNavTitle];
}

-(void)setQwNavBarStatus:(QWNavBarStatus)qwNavBarStatus {
    if (_qwNavBarStatus == qwNavBarStatus) {
        return;
    }
    _qwNavBarStatus = qwNavBarStatus;
    [self qw_refreshNavBarUI];
    [self setNeedsStatusBarAppearanceUpdate];
}

-(void) qw_refreshNavBarUI {
    QWNavBarStatusAttributes *statusAttributes = [self qw_navBarStatusAttributesDict][@(_qwNavBarStatus)];
    if (statusAttributes) {
        [self.qwNavigationBar setBackgroundImage:statusAttributes.barBackgroundImage forBarMetrics:UIBarMetricsDefault];
        [self.qwNavigationBar setTranslucent:statusAttributes.isTranslucent];
        [self.qwNavigationBar setTitleTextAttributes:statusAttributes.titleTextAttributes];
        [self.qwNavigationBar setShadowImage:statusAttributes.shadowImage];
        [self.qwNavigationBar setTintColor:statusAttributes.tintColor];
        [[UIApplication sharedApplication] setStatusBarStyle:statusAttributes.statusBarStyle];
    }
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    QWNavBarStatusAttributes *statusAttributes = [self qw_navBarStatusAttributesDict][@(_qwNavBarStatus)];
    if (statusAttributes) {
        return [statusAttributes statusBarStyle];
    }
    return UIStatusBarStyleDefault;
}

-(void) qw_popExec:(id) sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void) qw_refreshUIElement {
    if ([self qw_existNavigationBar]) {
        if (!_qwNavigationBar) {
            UINavigationBar *bar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64.0f)];
            UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:_qwNavTitle];
            _qwNavigationItem = item;
            
            [bar setItems:[NSArray arrayWithObjects:item, nil]];
            if ([self.navigationController.viewControllers count] > 1) {
                UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:NULL];
                negativeSpacer.width = -10;
                UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_nav_back_white"] style:UIBarButtonItemStylePlain target:self action:@selector(qw_popExec:)];
                [self.qwNavigationItem setLeftBarButtonItems:@[negativeSpacer,backItem]];
            }
            
            [bar setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleBottomMargin];
            [self.view addSubview:bar];
            _qwNavigationBar = bar;
        }
        if (!_qwView) {
            UIView *v = [[UIView alloc] initWithFrame:([self edgesForExtendedLayout] & UIRectEdgeTop) ? self.view.bounds : CGRectMake(0, 64.0f, self.view.bounds.size.width, self.view.bounds.size.height - 64.0f)];
            [v setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
            [self.view insertSubview:v atIndex:0];
            _qwView = v;
        }
    } else {
        _qwView = self.view;
    }
    [self qw_refreshNavBarUI];
}

-(void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    if ([self qw_existNavigationBar]) {
        [_qwNavigationBar setFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64.0f)];
        if ([self edgesForExtendedLayout] & UIRectEdgeTop) {
            [_qwView setFrame:self.view.bounds];
        } else {
            [_qwView setFrame:CGRectMake(0, 64.0f, self.view.bounds.size.width, self.view.bounds.size.height - 64.0f)];
        }
    }
}

-(BOOL)qw_existNavigationBar {
    return YES;
}

-(NSDictionary *)qw_navBarStatusAttributesDict {
    return nil;
}

#pragma mark IQWRouterViewController
-(void) viewController:(UIViewController*) viewController routerParams:(NSDictionary*) params {
    [self.routerDelegate viewController:viewController routerParams:params];
}
-(id) initWithURL:(NSString*) URL routerParams:(NSDictionary*) params {
    self = [super init];
    if (self) {
        self.routParams = params;
    }
    return self;
}
-(void) setRouterDelegate:(id<IQWRouterViewControllerDelegate>) delegate {
    self.routerDelegate = delegate;
}

-(id<IQWRouterViewControllerDelegate>) routerDelegate {
    return _routDelegate;
}

@end
